﻿using System;
using System.Collections.Generic;
//using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace S_Wings
{
  class Button {
    protected Texture2D sprite;
    protected String texture;
    protected Vector2 position;
    protected bool loaded = false;

    public Button() {
      sprite = null;
      texture = "default_button";
      position = Vector2.Zero;
      loaded = false;
    }

    public Button(Texture2D s) {
      sprite = s;
      texture = s.Name;
      position = Vector2.Zero;
      loaded = true;
    }

    public Button(String s) {
      texture = s;
      position = Vector2.Zero;
      loaded = false;
    }

    public virtual Rectangle getSize() {
      return new Rectangle((int)position.X, (int)position.Y, sprite.Width, sprite.Height);
    }

    public virtual void Load(ContentManager Content) {
      if (loaded)
        return;
      sprite = Content.Load<Texture2D>(texture);
    }

    public virtual void Update(double elapsed, KeyboardState ks, GamePadState gs, MouseState ms) { }
    
    public virtual void Draw(SpriteBatch sb) {
      sb.Draw(sprite, position, Color.White);
    }
  }

  class PushButton : Button {
    Rectangle[] state_source = new Rectangle[3];
    PushState state;

    enum PushState {
      NULL = 0,
      HOVER = 1,
      DOWN = 2,
    };

    public PushButton(Texture2D s) : base(s) {
      int h = s.Height/3;
      int w = s.Width;
      state_source[(int)PushState.NULL] = new Rectangle(0, 0, w, h);
      state_source[(int)PushState.NULL] = new Rectangle(0, h, w, h);
      state_source[(int)PushState.NULL] = new Rectangle(0, 2 * h, w, h);
    }

    public PushButton(String s, Rectangle r) : base(s) {
      int h = r.Height / 3;
      int w = r.Width;
      state_source[(int)PushState.NULL] = new Rectangle(0, 0, w, h);
      state_source[(int)PushState.NULL] = new Rectangle(0, h, w, h);
      state_source[(int)PushState.NULL] = new Rectangle(0, 2 * h, w, h);
    }

    public override void Update(double elapsed, KeyboardState ks, GamePadState gs, MouseState ms) {
      // Will only update based on mouse.
      // Keyboard and GamePad updates are handled by the group
      Rectangle location = new Rectangle((int)position.X, (int)position.Y, sprite.Width, sprite.Height/3);
      if (location.Contains(ms.X, ms.Y)) {
        if (ms.LeftButton == ButtonState.Pressed) {
          // Pressing button
          state = PushState.DOWN;
        } else {
          // Just hovering
          state = PushState.HOVER;
        }
      }
    }

    public override void Draw(SpriteBatch sb) {
      sb.Draw(sprite, position, state_source[(int)state], Color.White);
    }
  }

  public class ButtonGroup {
    protected List<Button> group;
    protected List<int> LR_order;
    protected List<int> UD_order;
    protected int index;

    public ButtonGroup() {
      group = new List<Button>();
      LR_order = new List<int>();
      UD_order = new List<int>();
      index = -1;
    }

    public ButtonGroup(Button b) {
      group = new List<Button>();
      group.Add(b);
      LR_order = new List<int>();
      LR_order.Add(0);
      UD_order = new List<int>();
      LR_order.Add(0);
      index = 0;
    }

    public ButtonGroup(List<Button> b, List<int> order1, List<int> order2) {
      group = b;
      LR_order = order1;
      UD_order = order2;
      index = 0;
    }

    public void add(Button b) {
      int size = group.Count;
      group.Add(b);
      LR_order.Add(size);
      UD_order.Add(size);
      index++;
    }

    public int update(KeyboardState ks, GamePadState gs, MouseState ms) {
      // Handle Keyboard and GamePad updates
      // Make sure only one is selected
      // Returns the selected button index?
      return -1;
    }
  }
}
